﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Game
{
    enum ACTION_MODE { ATK_MODE, DEF_MODE, SLOWATK_MODE, SLOWDEF_MODE };
    static class ActionSet
    {
        const int SlowDis = 2;
        static public Dictionary<string, int> DoAction(Slime s, UI ui, ACTION_MODE mode, int side, bool show)
        {
            switch (mode) { 
                case ACTION_MODE.ATK_MODE:
                   return DoATK(s, ui, side, show);
                    
                case ACTION_MODE.DEF_MODE:
                    return DoDEF(s, ui, side, show);
                case ACTION_MODE.SLOWATK_MODE:
                    return DoSlowATK(s, ui, side, show);
                case ACTION_MODE.SLOWDEF_MODE:
                    return DoSlowDEF(s, ui, side, show);
            }
            return null;
        }
        static public int TestATK(UI ui, int side)
        {
            int ret = 0;
            SlimeGame tempgame = new SlimeGame(ui.GameData);
            UI tempUI =tempgame.GetUI();
            Player P1 = tempUI.GetPlayer(side);
            Player P2 = tempUI.GetPlayer(side^1);
            ret = P2.Slimes[0].HP;
            for (int i = 0; i < P1.Slimes.Count; i++)
            {
                Slime S = P1.Slimes[i];
                Position P = tempUI.Map.Chase(S.Pos, P2.Slimes[0].Pos, S.MovDis, 1, S.Side); //往大史萊姆移動
                tempUI.Move(S, P,false);
                tempUI.Atk(S, P2.Slimes[0], false);
            }
            ret -= P2.Slimes[0].HP;
            return ret;
        }


        static Dictionary<string, int> DoATK(Slime S, UI ui, int side, bool show)
        {
            bool active = S.Active;
            Dictionary<string, int> ret = new Dictionary<string, int>();
            Player _P1 = ui.GetPlayer(side);
            Player _P2 = ui.GetPlayer(side ^ 1);
            Position P = ui.Map.Search(S); //可以攻擊到最少血敵人的位置
            if (P.X == -1 && P.Y == -1)
            {
                P = S.Pos;
                Slime temp = _P2.Slimes[_P2.Closest(P)]; //找出最近的敵人
                P = ui.Map.Chase(S.Pos, temp.Pos, S.MovDis, S.Range, S.Side);
            }
            Slime Target = _P2.Slimes[_P2.Closest(P)]; //找出最近的敵人


            ui.Move(S, P, show);
            ui.Atk(S, Target, show);


            ret.Add("x", S.Pos.X);
            ret.Add("y", S.Pos.Y);
            ret.Add("atk_id", S.ID);
            if (active && !S.Active)
            {
                ret.Add("target_id", Target.ID);
            }
            else
            {
                ret.Add("target_id", -1);
            }
            S.Stay();
            return ret;
        }
        static Dictionary<string, int> DoSlowATK(Slime S, UI ui, int side, bool show)
        {
            bool active = S.Active;
            Dictionary<string, int> ret = new Dictionary<string, int>();
            Player _P1 = ui.GetPlayer(side);
            Player _P2 = ui.GetPlayer(side ^ 1);
            Position P = ui.Map.Search(S); //可以攻擊到最少血敵人的位置
            if (P.X == -1 && P.Y == -1)
            {
                P = S.Pos;
                Slime temp = _P2.Slimes[_P2.Closest(P)]; //找出最近的敵人
                P = ui.Map.Chase(S.Pos, temp.Pos, SlowDis, S.Range, S.Side);
            }
            Slime Target = _P2.Slimes[_P2.Closest(P)]; //找出最近的敵人


            ui.Move(S, P, show);
            ui.Atk(S, Target, show);


            ret.Add("x", S.Pos.X);
            ret.Add("y", S.Pos.Y);
            ret.Add("atk_id", S.ID);
            if (active && !S.Active)
            {
                ret.Add("target_id", Target.ID);
            }
            else
            {
                ret.Add("target_id", -1);
            }
            S.Stay();
            return ret;
        }
        static Dictionary<string, int> DoDEF(Slime S, UI ui, int side, bool show)
        {
            bool active = S.Active;
            Dictionary<string, int> ret = new Dictionary<string, int>();
            Player _P1 = ui.GetPlayer(side);
            Player _P2 = ui.GetPlayer(side ^ 1);
            Slime Target = _P2.Slimes[_P2.Closest(S.Pos)]; //找出最近的敵人
            ui.Atk(S, Target, show);

            Position P = ui.Map.Chase(S.Pos, _P1.Slimes[0].Pos, S.MovDis, 1, S.Side); //往大史萊姆移動
            if (P.X != -1 && P.Y != -1)
            {
                ui.Move(S, P, show);
            }

            ret.Add("x", S.Pos.X);
            ret.Add("y", S.Pos.Y);
            ret.Add("atk_id", S.ID);
            if (active && !S.Active)
            {
                ret.Add("target_id", Target.ID);
            }
            else
            {
                ret.Add("target_id", -1);
            }
            S.Stay();
            return ret;
        }
        static Dictionary<string, int> DoSlowDEF(Slime S, UI ui, int side, bool show)
        {
            bool activeFlag = S.Active;
            Dictionary<string, int> ret = new Dictionary<string, int>();
            Player _P1 = ui.GetPlayer(side);
            Player _P2 = ui.GetPlayer(side ^ 1);

            Slime Target = _P2.Slimes[_P2.Closest(S.Pos)]; //找出最近的敵人
            if (ui.Map.Distance(S.Pos, Target.Pos) - 2 <= ui.Map.Distance(S.Pos, _P1.Slimes[0].Pos)) //比較大史和敵人距離
            {
                //Target = Target ;
                Position P = ui.Map.Chase(S.Pos, Target.Pos, S.MovDis, 1, S.Side);
                ui.Move(S, P, show);
                ui.Atk(S, Target, show);
            }
            else {
                Target = _P1.Slimes[0];
                Position P = ui.Map.Chase(S.Pos, Target.Pos, S.MovDis, 1, S.Side);
                ui.Move(S, P, show);
            }
            ret.Add("x", S.Pos.X);
            ret.Add("y", S.Pos.Y);
            ret.Add("atk_id", S.ID);
            if (activeFlag && !S.Active)
            {
                ret.Add("target_id", Target.ID);
            }
            else
            {
                ret.Add("target_id", -1);
            }
            S.Stay();
            return ret;
        }
        
    }
}


